<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="启动新进程的方式1.systemsystem 并不是启动新进程的最佳选择。使用他启动新进程，依赖于shell和环境变量。 实验 12345678910#include &amp;lt;stdlib.h&amp;gt;#include &amp;lt;stdio.h&amp;gt;int main()&amp;#123;  printf(&quot;Start Run px:\n&quot;);  system(&quot;ps ax&quot;);  printf(&quot;Done">
<meta name="keywords" content="Linux基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux进程">
<meta property="og:url" content="http://yoursite.com/2018/04/25/Linux进程介绍/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="启动新进程的方式1.systemsystem 并不是启动新进程的最佳选择。使用他启动新进程，依赖于shell和环境变量。 实验 12345678910#include &amp;lt;stdlib.h&amp;gt;#include &amp;lt;stdio.h&amp;gt;int main()&amp;#123;  printf(&quot;Start Run px:\n&quot;);  system(&quot;ps ax&quot;);  printf(&quot;Done">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-20T13:23:23.041Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux进程">
<meta name="twitter:description" content="启动新进程的方式1.systemsystem 并不是启动新进程的最佳选择。使用他启动新进程，依赖于shell和环境变量。 实验 12345678910#include &amp;lt;stdlib.h&amp;gt;#include &amp;lt;stdio.h&amp;gt;int main()&amp;#123;  printf(&quot;Start Run px:\n&quot;);  system(&quot;ps ax&quot;);  printf(&quot;Done">
  <link rel="canonical" href="http://yoursite.com/2018/04/25/Linux进程介绍/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Linux进程 | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/04/25/Linux进程介绍/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">Linux进程

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-04-25 21:23:04" itemprop="dateCreated datePublished" datetime="2018-04-25T21:23:04+00:00">2018-04-25</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="启动新进程的方式"><a href="#启动新进程的方式" class="headerlink" title="启动新进程的方式"></a>启动新进程的方式</h3><h4 id="1-system"><a href="#1-system" class="headerlink" title="1.system"></a>1.system</h4><p>system 并不是启动新进程的最佳选择。使用他启动新进程，依赖于shell和环境变量。</p>
<p><strong>实验</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"Start Run px:\n"</span>);</span><br><span class="line">  system(<span class="string">"ps ax"</span>);</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"Done~\n"</span>);</span><br><span class="line">  <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<p>输出：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">  PID TTY      STAT   TIME COMMAND</span><br><span class="line">    1 ?        Ss     0:00 /sbin/init</span><br><span class="line">    2 ?        S      0:00 [kthreadd]</span><br><span class="line">    3 ?        S      0:00 [ksoftirqd/0]</span><br><span class="line">  ...</span><br><span class="line"> 3834 ?        S      0:00 /home/admini/private/9.dailystudy/04-26/system</span><br><span class="line"> 3835 ?        S      0:00 sh -c ps ax</span><br><span class="line"> 3836 ?        R      0:00 ps ax</span><br><span class="line">Start Run px:</span><br><span class="line">Done~</span><br><span class="line">[Finished in 1.1s]</span><br></pre></td></tr></table></figure>

<p>将<code>system(&quot;ps ax&quot;);</code>替换成<code>system(&quot;ps ax &amp;&quot;)</code>;</p>
<p>运行结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Start Run px:</span><br><span class="line">Done~</span><br><span class="line">  PID TTY      STAT   TIME COMMAND</span><br><span class="line">    1 ?        Ss     0:00 /sbin/init</span><br><span class="line">    2 ?        S      0:00 [kthreadd]</span><br><span class="line">    3 ?        S      0:00 [ksoftirqd/0]</span><br><span class="line">  ...</span><br><span class="line"> 3862 ?        S      0:00 gnome-pty-helper</span><br><span class="line"> 3863 pts/0    Ss+    0:00 bash</span><br><span class="line"> 3877 ?        S      0:00 [kworker/u4:0]</span><br><span class="line"> 3888 ?        Z      0:00 [bash] &lt;defunct&gt;</span><br><span class="line"> 3898 ?        R      0:00 ps ax</span><br><span class="line">[Finished in 0.1s]</span><br></pre></td></tr></table></figure>

<h4 id="2-exec-替换进程印象"><a href="#2-exec-替换进程印象" class="headerlink" title="2.exec 替换进程印象"></a>2.exec 替换进程印象</h4><p>exec系列函数是由一组相关的函数组成，只是在启动方式和参数上不一致。<br>分为两大类:</p>
<ol>
<li>execl 参数个数不定，最后一个以空指针结束。</li>
<li>execv 参数以数组形式提供。</li>
</ol>
<p>以p结尾的函数，表示在PATH这个环境变量下找新的进程。</p>
<p><strong>实验</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">  printf(&quot;Start Run px:\n&quot;);</span><br><span class="line">  system(&quot;ps ax&quot;);</span><br><span class="line">  printf(&quot;Done~\n&quot;);</span><br><span class="line">  exit(0);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> PID TTY      STAT   TIME COMMAND</span><br><span class="line">   1 ?        Ss     0:00 /sbin/init</span><br><span class="line">   2 ?        S      0:00 [kthreadd]</span><br><span class="line">   3 ?        S      0:00 [ksoftirqd/0]</span><br><span class="line"></span><br><span class="line">4126 ?        S      0:00 bash -c gcc &apos;/home/admini/private/9.dailystudy/04-26/exec.c&apos; -o &apos;/home/admini/private/9.dailystudy/04-26/exec&apos; &amp;&amp; &apos;/home/admini/private/9.dailystudy/04-26/exec&apos;</span><br><span class="line">4134 ?        R      0:00 ps ax</span><br></pre></td></tr></table></figure>

<p>进程中没有原来的进程，而且输出结果中原来的输出也没有。</p>
<h4 id="3-fork-复制进程映像"><a href="#3-fork-复制进程映像" class="headerlink" title="3.fork 复制进程映像"></a>3.fork 复制进程映像</h4><p>fork调用后，用同样的代码，创建一个子进程，子进程中fork调用返回0，父进程可以通过这点来判断谁是父，谁是子。<br>父子之间的变量是相互独立的。</p>
<p><strong>实验</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;sys/types.h&gt;</span><br><span class="line">#include &lt;unistd.h&gt;</span><br><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line"></span><br><span class="line">int main() </span><br><span class="line">&#123;</span><br><span class="line">  pid_t pid;</span><br><span class="line">  char *message;</span><br><span class="line">  int n =0;</span><br><span class="line"></span><br><span class="line">  printf(&quot;========start=========\n&quot;);</span><br><span class="line"></span><br><span class="line">  pid = fork();</span><br><span class="line"></span><br><span class="line">  switch(pid)</span><br><span class="line">  &#123;</span><br><span class="line">    case -1:</span><br><span class="line">      message = &quot;Error\n&quot;;</span><br><span class="line">      break;</span><br><span class="line">    case 0:</span><br><span class="line">      message = &quot;I&apos;m Child~\n&quot;;</span><br><span class="line">      n= n + 3;</span><br><span class="line">      break;</span><br><span class="line">    default:</span><br><span class="line">      message = &quot;I&apos;m Fahter\n&quot;;</span><br><span class="line">      n = n +2;</span><br><span class="line">      break;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  printf(&quot;%s&quot;,message);</span><br><span class="line">  printf(&quot;%d\n&quot;,n);</span><br><span class="line">  return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">========start=========</span><br><span class="line">I&apos;m Fahter</span><br><span class="line">2</span><br><span class="line">========start=========</span><br><span class="line">I&apos;m Child~</span><br><span class="line">3</span><br><span class="line">[Finished in 0.0s]</span><br></pre></td></tr></table></figure>

<p>如果加上wait，父进程等待子进程退出。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">  ...</span><br><span class="line">printf(&quot;%s&quot;,message);</span><br><span class="line">printf(&quot;%d\n&quot;,n);</span><br><span class="line"></span><br><span class="line">if (0 != pid) </span><br><span class="line">&#123;</span><br><span class="line">  int stat_val;</span><br><span class="line">  pid_t child_pid;</span><br><span class="line">  child_pid = wait(&amp;stat_val);</span><br><span class="line"></span><br><span class="line">  printf(&quot;%s\n&quot;, &quot;child finish&quot;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">return 0;</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">========start=========</span><br><span class="line">I&apos;m Child~</span><br><span class="line">3</span><br><span class="line">========start=========</span><br><span class="line">I&apos;m Fahter~</span><br><span class="line">2</span><br><span class="line">child finish</span><br><span class="line">[Finished in 0.1s]</span><br></pre></td></tr></table></figure>

<p>可以看到父进程的log输出在子进程后面。</p>
<h4 id="僵尸进程"><a href="#僵尸进程" class="headerlink" title="僵尸进程"></a>僵尸进程</h4><p>fork 处理来的子进程比父进程先结束，但是由于需要等待父进程，所以还在在进程表中。<br>这种情况他就是一个僵尸进程。</p>
<p>如果父进程异常终止，则子进程会挂到init进程中，永远没有机会释放。</p>
<p>wait是父进程等待挂起。waitpid不会挂起父进程，可以通过周期性的查询知道子进程的状态。</p>
<h2 id="启动线程的方法："><a href="#启动线程的方法：" class="headerlink" title="启动线程的方法："></a>启动线程的方法：</h2><p>int pthread_create(&amp;threadid,NULL,thread_function,(void*)message);<br>第二个参数是线程的属性，一般情况设置为NULL，第3个参数是线程函数。</p>
<p>int pthread_join(threadid,&amp;threadresult);<br>等待线程的结束，并取得返回退出的结果。退出的结果存在threadresult</p>
<p>pthread_exit(“end thread”);<br>结束线程，并返回结果。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#include &lt;pthread.h&gt;</span><br><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line">#include &lt;unistd.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line"></span><br><span class="line">void *thread_function(void *arg);</span><br><span class="line"></span><br><span class="line">char message[] = &quot;hello world&quot;;</span><br><span class="line"></span><br><span class="line">int main() &#123;</span><br><span class="line">	int res;</span><br><span class="line">	pthread_t threadid;</span><br><span class="line">	// 不明白用成员变量指针为什么可以存储变量，不会出现错误修改内存的问题吗？</span><br><span class="line">	void *threadresult;</span><br><span class="line"></span><br><span class="line">	printf(&quot;create thread\n&quot;);</span><br><span class="line"></span><br><span class="line">	res = pthread_create(&amp;threadid,NULL,thread_function,(void*)message);</span><br><span class="line"></span><br><span class="line">	if (res != 0) &#123;</span><br><span class="line">		printf(&quot;create thread error!&quot;);</span><br><span class="line">		exit(1);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	printf(&quot;wait thread end\n&quot;);</span><br><span class="line">	res = pthread_join(threadid,&amp;threadresult);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	if (res != 0) &#123;</span><br><span class="line">		printf(&quot;join thread error!&quot;);</span><br><span class="line">		exit(1);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	printf(&quot;get message from thread:%s\n&quot;,(char*)threadresult);</span><br><span class="line">	printf(&quot;current message is %s&quot;,message);</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void * thread_function(void *arg) &#123;</span><br><span class="line">	int n = 3;</span><br><span class="line">	while(n&gt;0) &#123;</span><br><span class="line">		n--;</span><br><span class="line">		sleep(1);</span><br><span class="line">		printf(&quot;thread work!modify the message\n&quot;);</span><br><span class="line">		strcpy(message,&quot;Bye!\n&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	pthread_exit(&quot;end thread&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc ./thread.c -o thread -lpthread</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight plain"><figcaption><span>sem_post(sem_t *sem);</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">create thread</span><br><span class="line">wait thread end</span><br><span class="line">thread work!modify the message</span><br><span class="line">thread work!modify the message</span><br><span class="line">thread work!modify the message</span><br><span class="line">get message from thread:end thread</span><br><span class="line">current message is Bye!</span><br></pre></td></tr></table></figure>

<h4 id="线程同步"><a href="#线程同步" class="headerlink" title="线程同步"></a>线程同步</h4><h4 id="信号量"><a href="#信号量" class="headerlink" title="信号量"></a>信号量</h4><p>信号量线程同步包括二进制信号量和计数信号量。</p>
<p>pshared是一直为0pshared<br>int sem_init(sem_t *sem, int pshared, unsigned int value);</p>
<p>int sem_post(sem_t *sem);</p>
<p>int sem_wait(sem_t *sem);</p>
<p>int sem_destroy(sem_t *sem);</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/Linux基础/" rel="tag"># Linux基础</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2018/04/21/Scrapy-教程/" rel="next" title="Scrapy 教程">
                  <i class="fa fa-chevron-left"></i> Scrapy 教程
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2018/05/17/树莓派上的Web Cam/" rel="prev" title="树莓派上的Web Cam">
                  树莓派上的Web Cam <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动新进程的方式"><span class="nav-number">1.</span> <span class="nav-text">启动新进程的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-system"><span class="nav-number">1.1.</span> <span class="nav-text">1.system</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-exec-替换进程印象"><span class="nav-number">1.2.</span> <span class="nav-text">2.exec 替换进程印象</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-fork-复制进程映像"><span class="nav-number">1.3.</span> <span class="nav-text">3.fork 复制进程映像</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#僵尸进程"><span class="nav-number">1.4.</span> <span class="nav-text">僵尸进程</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动线程的方法："><span class="nav-number"></span> <span class="nav-text">启动线程的方法：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#线程同步"><span class="nav-number">0.1.</span> <span class="nav-text">线程同步</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#信号量"><span class="nav-number">0.2.</span> <span class="nav-text">信号量</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
